layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 积分比例管理表管理
     */
    var IntegralBeliel = {
        tableId: "integralBelielTable"
    };

    /**
     * 初始化表格的列
     */
    IntegralBeliel.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, align: 'center',title: '编号'},
            {field: 'name', sort: true, align: 'center',title: '名称'},
            {
                field: 'integralBeliel', sort: true, title: '积分获取的比例',align: 'center', templet: function (d) {
                    return '<a>' + d.integralBeliel*100 + '%</a>'
                }
            },
            {
                field: 'payBeliel', sort: true, title: '积分消费的比例', align: 'center',templet: function (d) {
                    return '<a>' + d.payBeliel + '积分 = 1元</a>'
                }
            },
            {field: 'state', sort: true, title: '是否开启', templet: '#statusTpls'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    IntegralBeliel.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(IntegralBeliel.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    IntegralBeliel.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/integralBeliel/add';
    };

    /**
     * 导出excel按钮
     */
    IntegralBeliel.exportExcel = function () {
        var checkRows = table.checkStatus(IntegralBeliel.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    IntegralBeliel.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/integralBeliel/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    IntegralBeliel.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/integralBeliel/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(IntegralBeliel.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + IntegralBeliel.tableId,
        url: Feng.ctxPath + '/integralBeliel/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: IntegralBeliel.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        IntegralBeliel.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        IntegralBeliel.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        IntegralBeliel.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + IntegralBeliel.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            IntegralBeliel.openEditDlg(data);
        } else if (layEvent === 'delete') {
            IntegralBeliel.onDeleteItem(data);
        }
    });
    /**
     * 修改状态
     *
     *
     */
    IntegralBeliel.changeUserStatus = function (userId, checked) {
        if (checked == 1) {
            var ajax = new $ax(Feng.ctxPath + "/integralBeliel/editItems", function (data) {
                Feng.success("开启成功!");
                table.reload(IntegralBeliel.tableId);
            }, function (data) {
                Feng.error("开启失败!");
                table.reload(IntegralBeliel.tableId);
            });
            ajax.set("id", userId);
            ajax.set("state", 1);
            ajax.start();
        } else if (checked == 0) {
            var ajax = new $ax(Feng.ctxPath + "/integralBeliel/editItems", function (data) {
                Feng.error("至少开启一项！");
                table.reload(IntegralBeliel.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.responseJSON.message + "!");
                table.reload(IntegralBeliel.tableId);
            });
            ajax.set("id", userId);
            ajax.set("state", 0);
            ajax.start();
        }
    };

    // 修改状态
    form.on('switch(status)', function (obj) {

        var userId = obj.elem.value;
        var checked = obj.elem.checked ? 1 : 0;

        IntegralBeliel.changeUserStatus(userId, checked);
    });
});

function changeStatus(id, status) {
    var actCh;
    var cmd;
    if (status == 1) {
        cmd = '0';
        actCh = "确认禁用吗？";
    } else if (status == 0) {
        cmd = '1';
        actCh = "确认恢复吗？";
    }
    layer.confirm(actCh, {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: Feng.ctxPath + "/integralBeliel/editItems",
            type: "post",
            data: {
                'id': id,
                'state': cmd
            },
            success: function (r) {
                //alert(JSON.stringify(r));
                if (r.code == 200) {
                    Feng.success("操作成功!");
                    window.location.href = Feng.ctxPath + '/integralBeliel';
                } else if (r.code == 201) {
                    Feng.error("至少开启一项！");
                    window.location.href = Feng.ctxPath + '/integralBeliel';
                }
            }
        });
    })
}

